Imports KanBan.Pgd.Infrastructure.EventLog
Imports KanBan.Pgd.Domain
Imports KanBan.Pgd.Domain.Exceptions

Namespace Exceptions

    Public Class ExceptionHandler

        Public Shared Function HandleException(ByVal ex As Exception) As IList(Of ApplicationError)

            Dim errorList As New List(Of ApplicationError)

            If ex.GetType() Is GetType(EntityValidationException) Then

                Dim evx As EntityValidationException = CType(ex, EntityValidationException)

                For Each err As EntityValidationError In evx.ValidationErrors
                    errorList.Add(New ApplicationError(err.Key, err.Message))
                Next

                EventLogger.Log(EventLevel.Info, ex)

            ElseIf ex.GetType() Is GetType(BusinessRuleException) Then

                errorList.Add(New ApplicationError("BusinessException", ex.Message))
                EventLogger.Log(EventLevel.Info, ex)

            ElseIf ex.GetType() Is GetType(TechnicalException) Then

                errorList.Add(New ApplicationError("TechnicalException", ex.Message))
                EventLogger.Log(EventLevel.Error, ex)

            Else

                errorList.Add(New ApplicationError("UnknownException", ex.Message))
                EventLogger.Log(EventLevel.Error, ex)

            End If

            Return errorList

        End Function

    End Class

End Namespace
